package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.PaymentPlatforms;
import com.example.mall.dto.PaymentPlatformsDto;
import com.example.mall.mapper.PaymentPlatformsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.PaymentPlatformsQuery;
import com.example.mall.service.IPaymentPlatformsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 支付平台表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-09
 */
@Service
public class PaymentPlatformsServiceImpl extends ServiceImpl<PaymentPlatformsMapper, PaymentPlatforms> implements IPaymentPlatformsService {

    @Autowired
    private PaymentPlatformsMapper paymentPlatformsMapper;

    @Override
    public PageDTO<PaymentPlatformsDto> queryPage(PaymentPlatformsQuery paymentPlatformsQuery) {
        if (paymentPlatformsQuery == null) {
            paymentPlatformsQuery = new PaymentPlatformsQuery();
        }
        Page<PaymentPlatforms> page = paymentPlatformsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<PaymentPlatforms> cartPage = lambdaQuery()
                .like(paymentPlatformsQuery.getPlatformId()!= null, PaymentPlatforms::getPlatformId, paymentPlatformsQuery.getPlatformId())
                .like(paymentPlatformsQuery.getIsActive()!= null, PaymentPlatforms::getIsActive, paymentPlatformsQuery.getIsActive())
                .like(paymentPlatformsQuery.getDescription()!= null, PaymentPlatforms::getDescription, paymentPlatformsQuery.getDescription())
                .like(paymentPlatformsQuery.getName()!= null, PaymentPlatforms::getName, paymentPlatformsQuery.getName())
                .orderByDesc(PaymentPlatforms::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, PaymentPlatformsDto.class);
    }

    @Override
    public PaymentPlatforms queryPaymentPlatformsById(Long platformId) {
        AssertUtil.isNotNull(platformId, "平台ID不能为空");
        return paymentPlatformsMapper.selectById(platformId);
    }

    @Override
    public List<PaymentPlatforms> queryPaymentPlatformsList() {
        return this.list();
    }

    @Override
    public void insert(PaymentPlatformsDto paymentPlatformsDto) {
        PaymentPlatforms paymentPlatforms = new PaymentPlatforms();
        CopyUtils.copyProperties(paymentPlatformsDto, paymentPlatforms);
        paymentPlatforms.setPlatformId(CodeGenerateUtils.generateUniqueId());
        paymentPlatforms.setIsActive(BaseConstants.EFFECTIVE_STATUS);
        paymentPlatforms.setCreateDate(new Date());
        paymentPlatforms.setUpdateDate(new Date());
        paymentPlatformsMapper.insert(paymentPlatforms);
    }

    @Override
    public void update(PaymentPlatformsDto paymentPlatformsDto) {
        AssertUtil.isNotNull(paymentPlatformsDto.getPlatformId(), "平台ID不能为空");
        PaymentPlatforms paymentPlatforms = paymentPlatformsMapper.selectById(paymentPlatformsDto.getPlatformId());
        AssertUtil.isNotNull(paymentPlatforms, "平台不存在");
        CopyUtils.copyProperties(paymentPlatformsDto, paymentPlatforms);
        paymentPlatforms.setUpdateDate(new Date());
        paymentPlatformsMapper.updateById(paymentPlatforms);
    }

    @Override
    public void delete(Long platformId) {
        AssertUtil.isNotNull(platformId, "平台ID不能为空");
        PaymentPlatforms paymentPlatforms = paymentPlatformsMapper.selectById(platformId);
        AssertUtil.isNotNull(paymentPlatforms, "平台不存在");
        paymentPlatforms.setIsActive(BaseConstants.LOSE_STATUS);
        paymentPlatforms.setUpdateDate(new Date());
        paymentPlatformsMapper.updateById(paymentPlatforms);
    }

    @Override
    public void deleteBatch(List<Long> platformIds) {
        AssertUtil.isNotNull(platformIds, "平台ID列表不能为空");
        List<PaymentPlatforms> paymentPlatforms = paymentPlatformsMapper.selectBatchIds(platformIds);
        AssertUtil.isNotNull(paymentPlatforms, "平台不存在");
        for (PaymentPlatforms paymentPlatform : paymentPlatforms) {
            paymentPlatform.setIsActive(BaseConstants.LOSE_STATUS);
            paymentPlatform.setUpdateDate(new Date());
            paymentPlatformsMapper.updateById(paymentPlatform);
        }
    }
}
